package leecode

import "fmt"

func CanCompleteCircuit(gas []int, cost []int) (result int) {
	// 创建一个切片，存储每个加油站的汽油储备与耗油量之差
	diff := make([]int, len(gas))
	for i := 0; i < len(gas); i++ {
		diff[i] = gas[i] - cost[i]
	}

	// 检查总和是否小于零，如果是，说明无法绕行整个路线
	total := 0
	for i := 0; i < len(diff); i++ {
		total += diff[i]
	}
	if total < 0 {
		return -1
	}
	fmt.Println(diff)

	// 找到起始加油站的位置
	start := 0
	currentSum := 0
	for i := 0; i < len(diff); i++ {
		currentSum += diff[i]
		if currentSum < 0 {
			// 如果当前和为负数，说明无法从当前位置出发，将起始位置更新为下一个加油站
			start = i + 1
			currentSum = 0
		}
	}

	return start
}
